//
//  SegmentControlCellData.m
//  18Birdies
//
//  Created by poleness on 2017/2/28.
//  Copyright © 2017年 Lc. All rights reserved.
//

#import "SegmentControlCellData.h"
#import "SegmentControlCell.h"
#import "LcFoundation.h"

@implementation SegmentControlItem

- (instancetype)initWithTitle:(NSString*)title {
  return [self initWithTitle:title clickable:YES];
}

- (instancetype)initWithTitle:(NSString*)title clickable:(BOOL)clickable {
  LC_CHECK_TRUE(title.length > 0);
  self = [super init];
  if (self) {
    self->_title = title;
    self->_clickable = clickable;
  }
  return self;
}

@end

@implementation SegmentControlCellData

+ (SegmentControlCellData*)cellDataWithSegmentControlItems:
                               (NSArray<SegmentControlItem*>*)segmentControlItems
                                          OnClickedHandler:(OnIndexClickedHandler)clickedHandler {
  return [self cellDataWithSegmentControlItems:segmentControlItems
                                 selectedIndex:-1
                                 segmentEnable:YES
                              OnClickedHandler:clickedHandler];
}

+ (SegmentControlCellData*)cellDataWithSegmentControlItems:
                               (NSArray<SegmentControlItem*>*)segmentControlItems
                                             selectedIndex:(int)selectedIndex
                                             segmentEnable:(BOOL)segmentEnable
                                          OnClickedHandler:(OnIndexClickedHandler)clickedHandler {
  LC_CHECK_TRUE(segmentControlItems.count > 0);
  SegmentControlCellData* cellData = [[self alloc] init];
  cellData->_segmentControlItems = segmentControlItems.copy;
  cellData.selectedIndex = selectedIndex;
  cellData.segmentEnable = segmentEnable;
  cellData->_clickedHandler = clickedHandler;
  cellData.controlHeight = 32.0;
  cellData.cellHeight = 44;
  return cellData;
}

- (Class)cellClazz {
  return SegmentControlCell.class;
}

- (NSString*)nibName {
  return @"SegmentControlCell";
}

- (void)refreshCell:(SegmentControlCell*)cell {
  [super refreshCell:cell];
  cell.clickedHandler = self.clickedHandler;
  cell.controlHeight.constant = self.controlHeight;
  [cell.segmentControl removeAllSegments];
  for (int i = 0; i < self.segmentControlItems.count; i++) {
    SegmentControlItem* segmentControlItem = self.segmentControlItems[i];
    [cell.segmentControl insertSegmentWithTitle:segmentControlItem.title atIndex:i animated:NO];
    if (!self.segmentEnable) {
      if (self.selectedIndex != i) {
        [cell.segmentControl setEnabled:NO forSegmentAtIndex:i];
      } else {
        [cell.segmentControl setEnabled:YES forSegmentAtIndex:i];
      }
    } else {
      [cell.segmentControl setEnabled:YES forSegmentAtIndex:i];
    }
    [self updateAttributesForCell:cell withData:segmentControlItem];
  }

  if (!self.segmentEnable) {
    self->_clickedHandler = nil;
  }
  if (self.selectedIndex != -1) {
    cell.segmentControl.selectedSegmentIndex = self.selectedIndex;
  }
  if (self.tintColor != nil) {
      cell.segmentControl.tintColor = self.tintColor;
    }
  }

- (void)updateAttributesForCell:(SegmentControlCell*)cell withData:(SegmentControlItem*)data {
  if (data.normalTitleFont != nil) {
    [cell.segmentControl setTitleTextAttributes:[self attributesWithLcFont:data.normalTitleFont]
                                       forState:UIControlStateNormal];
  }
  if (data.hightLightTitleFont != nil) {
    [cell.segmentControl setTitleTextAttributes:[self attributesWithLcFont:data.hightLightTitleFont]
                                       forState:UIControlStateSelected];
  }
  
  if (data.disableTitleFont == nil) {
    data.disableTitleFont = [data.normalTitleFont copy];
    if (data.disableTitleFont.color != nil) {
      data.disableTitleFont.fontColor([data.disableTitleFont.color colorWithAlphaComponent:0.5]);
    }
  }
  if (data.disableTitleFont != nil) {
    [cell.segmentControl setTitleTextAttributes:[self attributesWithLcFont:data.disableTitleFont]
                                       forState:UIControlStateDisabled];
  }
}

- (NSDictionary*)attributesWithLcFont:(LcFont*)lcFont {
  LC_CHECK_NOT_NULL(lcFont);
  NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
  if (lcFont.color != nil) {
    [attributes setObject:lcFont.color forKey:NSForegroundColorAttributeName];
  }
  [attributes setObject:lcFont.font forKey:NSFontAttributeName];
  return attributes;
}

@end
